//
//  SettingViewController.m
//  WBBaseProject
//
//  Created by 吴斌 on 2018/3/20.
//  Copyright © 2018年 吴斌. All rights reserved.
//

#import "SettingViewController.h"
#import "SettingCell.h"
#import "HeadImageCell.h"
#import "HeadImgViewController.h"
#import "PersonSignViewController.h"
#import "SDImageCache.h"

static NSString * const SettingCellReuseId = @"SettingCell";
static NSString * const HeadImageCellReuseId = @"HeadImageCell";


@interface SettingViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation SettingViewController

#pragma mark - 生命周期 Life Circle
- (void)viewDidLoad {
    [super viewDidLoad];  
    [self.view addSubview:self.tableView];
    self.navigationItem.title = @"设置";
    [self registerCell];
}

- (void)registerCell
{
    [self.tableView registerNib:[UINib nibWithNibName:SettingCellReuseId bundle:nil] forCellReuseIdentifier:SettingCellReuseId];
    [self.tableView registerNib:[UINib nibWithNibName:HeadImageCellReuseId bundle:nil] forCellReuseIdentifier:HeadImageCellReuseId];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 0) {
        return 3;
    }else{
        return 2;
    }
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath { 
    if (indexPath.section == 0) {
        
        if (indexPath.row == 0) {
            HeadImageCell *cell = [tableView dequeueReusableCellWithIdentifier:HeadImageCellReuseId forIndexPath:indexPath]; 
//            cell.titleLb.text = @"清除缓存";
//            cell.detailLb.text = @"2.4M";
            return cell;
        }else if(indexPath.row == 1){
            SettingCell *cell = [tableView dequeueReusableCellWithIdentifier:SettingCellReuseId forIndexPath:indexPath]; 
            cell.titleLb.text = @"姓名";
            cell.detailLb.text = @"暂未实名认证";
            cell.rightIV.hidden = YES;
            cell.detailLb.textColor = WB_RGBColor(85, 85, 85);
            return cell;
        }else{
            SettingCell *cell = [tableView dequeueReusableCellWithIdentifier:SettingCellReuseId forIndexPath:indexPath];
            cell.titleLb.text = @"个人签名";
            cell.detailLb.text = @"用一句话介绍自己";
            cell.rightIV.hidden = YES;
            cell.detailLb.textColor = WB_RGBColor(85, 85, 85);
            return cell;
        }
        
    }else{
        SettingCell *cell = [tableView dequeueReusableCellWithIdentifier:SettingCellReuseId forIndexPath:indexPath]; 
        if (indexPath.row == 0) {
            cell.titleLb.text = @"清除缓存";
            cell.rightIV.hidden = YES;
            cell.detailLb.text = [NSString stringWithFormat:@"%.2f M",(unsigned long)[[SDImageCache sharedImageCache] getSize]/1024.0/1024.0];
            cell.detailLb.textColor = WB_RGBColor(85, 85, 85);
        }else{
            cell.titleLb.text = @"版本信息";
            cell.detailLb.text = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
            cell.rightIV.hidden = YES;
            cell.detailLb.textColor = WB_RGBColor(85, 85, 85);
        }
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return  indexPath.section == 0 && indexPath.row == 0 ? 60.0f : 45.0f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 10)];
    view.backgroundColor = WB_RGBColor(240, 240, 240);
    return view;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        
        if (indexPath.row == 0) {
            
            [self showHeadImg];
        }else if (indexPath.row == 1){
            
        }else{
            [self showPersonSign];
        }
    }else{
        if (indexPath.row == 0) {
            
            [self cleanCache];
        }else if (indexPath.row == 1){
            [self showVersion];
        }
    }
}
- (void)showHeadImg
{
    HeadImgViewController *vc = [[HeadImgViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
    
}
- (void)showPersonSign
{
    PersonSignViewController *vc = [[PersonSignViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)cleanCache
{
    [[SDImageCache sharedImageCache]clearDiskOnCompletion:^{
        
        [Factory wb_showAlertWithTitle:@"清除成功"];
        [self.tableView reloadData];
    }];
}
- (void)showVersion
{
    
}
#pragma mark - 懒加载  Lazy Load
- (UITableView *)tableView
{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - kTabbarHeight) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = WB_RGBColor(240, 240, 240);
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [UIView new];
        _tableView.sectionFooterHeight = 0.f;
        _tableView.sectionHeaderHeight = 10.f;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}

@end
